﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DTO;
namespace CungMuaC5
{
    public partial class WebForm10 : System.Web.UI.Page
    {
        BUS_KhuyenMai bus_KhuyenMai = new BUS_KhuyenMai();
        BUS_Comment bus_CM = new BUS_Comment();

        string makm;
        int MaTK;
        protected void Page_Load(object sender, EventArgs e)
        {
            Load();
        }
        private void Load()
        {
            makm = Request.QueryString["MaKM"];
            List<DTO_KhuyenMai> list = bus_KhuyenMai.GetDetails(makm);
            List<DTO_ChiTietKM> chiTiet = new List<DTO_ChiTietKM>();
            if (list.Count > 0)
            {
                DTO_ChiTietKM km = new DTO_ChiTietKM();
                km.BatDauKM = list[0].BatDauKM;
                km.ClipQuangCao = list[0].ClipQuangCao;
                km.DiemNoiBat = list[0].DiemNoiBat;
                km.DKSuDung = list[0].DKSuDung;
                km.DoanhNghiep = list[0].DoanhNghiep;
                km.DonGiaCu = list[0].DonGiaCu;
                km.DonGiaMoi = list[0].DonGiaMoi;
                km.GioiThieuChiTiet = list[0].GioiThieuChiTiet;
                km.HinhAnh = list[0].HinhAnh;
                km.HinhThucGiaoSP = list[0].HinhThucGiaoSP;
                km.KetThucKM = list[0].KetThucKM;
                //km.LoaiKM = list[0].LoaiKM;
                km.MaKM = list[0].MaKM;
                km.MoTa = list[0].MoTa;
                km.SoNguoiCungMua = bus_KhuyenMai.laySoLuongDatHang(list[0].MaKM);
                km.TenKM = list[0].TenKM;
                km.TinhTrang = list[0].TinhTrang;
                chiTiet.Add(km);
            }
            ProductDetails.DataSource = chiTiet;
            ProductDetails.DataBind();
            int ma = int.Parse(makm);
            List<string> imgs = bus_KhuyenMai.getListImages(ma);
            for (int i = 0; i < imgs.Count&&i<2; i++)
            {
                Image img = (Image)ProductDetails.FindControl("ImageShow"+(i+1).ToString());
                img.ImageUrl = "~/Images/KhuyenMai/"+imgs[i];
            }


            DataList2.DataSource = bus_CM.TimKiemCMTheoKM(makm);
            DataList2.DataBind();
            if (Session.IsLogin() == 1)
            {
                MaTK = Session.GetCurrentUser().MaTK;
                //Label13.Text = Login.GetTenTK(MaTK);
            }
            else
            {

                TextBox17.Visible = false;
                ImageButton6.Visible = false;
            }
        }
        protected void ImageButton5_Click(object sender, ImageClickEventArgs e)
        {
            if (IsPostBack)
            {
                DTO_Comment dto = new DTO_Comment();
                dto.KhuyenMai = int.Parse(makm);
                dto.NguoiPost = MaTK;
                dto.NoiDung = TextBox17.Text;
                dto.ThoiGian = DateTime.Now;
                bool a = bus_CM.insertCM(dto);
                Load();
            }
        }

        protected void TextBox2_TextChanged(object sender, EventArgs e)
        {

        }

        protected void ImageButton6_Click(object sender, ImageClickEventArgs e)
        {
            DTO_Comment dto = new DTO_Comment();
            dto.KhuyenMai = int.Parse(makm);
            dto.NguoiPost = MaTK;
            dto.NoiDung = TextBox17.Text;
            dto.ThoiGian = DateTime.Now;
            bool a = bus_CM.insertCM(dto);
            if (a == true)
            {
                string url = "~/Products/ProductDetail.aspx?makm=" + makm;
                Response.Redirect(url);
            }
        }


    }
}